package com.song.pj.brand.controller;

import com.song.pj.brand.pojo.Brand;
import com.song.pj.brand.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class BrandController {
    @Autowired
    private BrandService brandService;
    //http://localhost/brand/doFindBrands?name=tcl 传统方式url
    //http://localhost/brand/doFindBrands/name=tcl rest风格url
    //rest风格（一种软件构架编码风格）的url定义
    //其语法为：a/b/{c}/d/{e},在url可以定义变量，这个变量需要使用{}括起来
    //rest风格可以更好实现跨平台
    //@PathVariable注解用于修饰方法参数，目的是告诉spring mvc，参数的值来自url

    @GetMapping(value = {"brand/doFindBrands","brand/doFindBrands/{name}"})//rest风格
    //Model封装响应结果
    //@PathVariable(required = false)表示修饰的参数name的值必须来自GetMapping中的urll
    public String doFindBrands(@PathVariable(required = false) String name, Model model){
        List<Brand> list = brandService.findBrands(name);
        model.addAttribute("list", list);
        return "brand/brand";//第一个brand为目录，第二个brand为view name
    }
    @GetMapping(value = {"brand/doDeleteBrands","brand/doDeleteBrands/{id}"})
    public String doDeleteBrands(@PathVariable(required = false) Integer id,Model model){
        int rows = brandService.deleteBrands(id);
        List<Brand> list = brandService.findBrands(null);
        model.addAttribute("list",list);
        System.out.println(rows);
        return "brand/brand";
    }
    @GetMapping("/brand/doAddUI")
    public String doAddUI(){
        return "brand/brand-add";
    }
    @PostMapping("/brand/doSaveBrand")
    public String doSaveBrand(Brand brand,Model model){
        int rows = brandService.insertBrands(brand);
        List<Brand> list=brandService.findBrands(null);
        model.addAttribute("list",list);
        return "brand/brand";
    }
    @RequestMapping("/brand/doUpdateBrand")
    public String doUpdateBrand(Brand brand,Model model){
        int rows = brandService.updateBrands(brand);
        List<Brand> list=brandService.findBrands(null);
        model.addAttribute("list",list);
        return "brand/brand";
    }
    @GetMapping("/brand/doFindById/{id}")
    public String doFindById(@PathVariable Integer id,Model model){
        Brand brand = brandService.findById(id);
        model.addAttribute("brand", brand);
        return "brand/barnd-update";
    }
}
